{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "import dominate\n",
    "from dominate.tags import meta, h3, table, tr, td, p, a, img, br\n",
    "import os\n",
    "\n",
    "\n",
    "class HTML:\n",
    "    \"\"\"This HTML class allows us to save images and write texts into a single HTML file.\n",
    "\n",
    "     It consists of functions such as <add_header> (add a text header to the HTML file),\n",
    "     <add_images> (add a row of images to the HTML file), and <save> (save the HTML to the disk).\n",
    "     It is based on Python library 'dominate', a Python library for creating and manipulating HTML documents using a DOM API.\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(self, web_dir, title, refresh=0):\n",
    "        \"\"\"Initialize the HTML classes\n",
    "\n",
    "        Parameters:\n",
    "            web_dir (str) -- a directory that stores the webpage. HTML file will be created at <web_dir>/index.html; images will be saved at <web_dir/images/\n",
    "            title (str)   -- the webpage name\n",
    "            refresh (int) -- how often the website refresh itself; if 0; no refreshing\n",
    "        \"\"\"\n",
    "        self.title = title\n",
    "        self.web_dir = web_dir\n",
    "        self.img_dir = os.path.join(self.web_dir, 'images')\n",
    "        if not os.path.exists(self.web_dir):\n",
    "            os.makedirs(self.web_dir)\n",
    "        if not os.path.exists(self.img_dir):\n",
    "            os.makedirs(self.img_dir)\n",
    "\n",
    "        self.doc = dominate.document(title=title)\n",
    "        if refresh > 0:\n",
    "            with self.doc.head:\n",
    "                meta(http_equiv=\"refresh\", content=str(refresh))\n",
    "\n",
    "    def get_image_dir(self):\n",
    "        \"\"\"Return the directory that stores images\"\"\"\n",
    "        return self.img_dir\n",
    "\n",
    "    def add_header(self, text):\n",
    "        \"\"\"Insert a header to the HTML file\n",
    "\n",
    "        Parameters:\n",
    "            text (str) -- the header text\n",
    "        \"\"\"\n",
    "        with self.doc:\n",
    "            h3(text)\n",
    "\n",
    "    def add_images(self, ims, txts, links, width=400):\n",
    "        \"\"\"add images to the HTML file\n",
    "\n",
    "        Parameters:\n",
    "            ims (str list)   -- a list of image paths\n",
    "            txts (str list)  -- a list of image names shown on the website\n",
    "            links (str list) --  a list of hyperref links; when you click an image, it will redirect you to a new page\n",
    "        \"\"\"\n",
    "        self.t = table(border=1, style=\"table-layout: fixed;\")  # Insert a table\n",
    "        self.doc.add(self.t)\n",
    "        with self.t:\n",
    "            with tr():\n",
    "                for im, txt, link in zip(ims, txts, links):\n",
    "                    with td(style=\"word-wrap: break-word;\", halign=\"center\", valign=\"top\"):\n",
    "                        with p():\n",
    "                            with a(href=os.path.join('images', link)):\n",
    "                                img(style=\"width:%dpx\" % width, src=os.path.join('images', im))\n",
    "                            br()\n",
    "                            p(txt)\n",
    "\n",
    "    def save(self):\n",
    "        \"\"\"save the current content to the HMTL file\"\"\"\n",
    "        html_file = '%s/index.html' % self.web_dir\n",
    "        f = open(html_file, 'wt')\n",
    "        f.write(self.doc.render())\n",
    "        f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "175\n",
      "dusseldorf_000209_000019_leftImg8bit.png\n"
     ]
    }
   ],
   "source": [
    "# read results\n",
    "webdir = 'middle_results'\n",
    "results_dir = {'Rain':'../results/CutrainPatch_MidCity_RH/',\n",
    "               'CycleGAN':'../results/middle_cycle/',\n",
    "               'CUT':'../results/MidCity_RH',\n",
    "               'CUT+contra':'../results/CutrainPatch_MidCity_RH/',\n",
    "              'Dual_ps1':'../results/middle_dual_ps1/',\n",
    "              'Dual_ps2':'../results/middle_dual_ps2/'}\n",
    "eg_dir = os.path.join(results_dir['CUT'],'test_latest','images','pred_B')\n",
    "img_names = os.listdir(eg_dir)\n",
    "import fnmatch\n",
    "img_names = fnmatch.filter(img_names, \"*.png\")\n",
    "print(len(img_names))\n",
    "print(img_names[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "180\n",
      "pic0211_24.png\n"
     ]
    }
   ],
   "source": [
    "# read results\n",
    "webdir = 'RO_results'\n",
    "results_dir = {'Rain':'../results/RO_DualPatch/',\n",
    "               'CUT':'../results/CUT_RO/',\n",
    "               ''\n",
    "               'Dual_ps4':'../results/RO_DualPatch/'\n",
    "              }\n",
    "img_names = os.listdir(eg_dir)\n",
    "eg_dir = os.path.join(results_dir['CUT'],'test_latest','images','pred_B')\n",
    "import fnmatch\n",
    "img_names = fnmatch.filter(img_names, \"*.png\")\n",
    "print(len(img_names))\n",
    "print(img_names[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "web = HTML(webdir,'Derain Result Compare')\n",
    "for img_name in img_names:\n",
    "    web.add_header(img_name)\n",
    "    imgs, labels, links = [], [], []\n",
    "    for key in results_dir.keys():\n",
    "        if key == 'Rain':\n",
    "            img_path = os.path.join(results_dir[key],'test_latest','images','O',img_name)\n",
    "        else:\n",
    "            img_path = os.path.join(results_dir[key],'test_latest','images','pred_B',img_name)\n",
    "        img_num = cv2.imread(img_path)\n",
    "        os.makedirs(os.path.join(webdir,'images',key), exist_ok=True)\n",
    "        save_path = os.path.join(webdir,'images',key,img_name)\n",
    "        href = os.path.join(key,img_name)\n",
    "        try:\n",
    "            cv2.imwrite(save_path,img_num)\n",
    "        except:\n",
    "            print(save_path,img_num)\n",
    "        imgs.append(href)\n",
    "        labels.append(key)\n",
    "        links.append(href)\n",
    "    web.add_images(imgs, labels, links)\n",
    "web.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# deal with cycle GAN dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "dirpath = r'../results/Cycle_MidCity_RH/test_latest/images/'\n",
    "import os\n",
    "import fnmatch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "175\n"
     ]
    }
   ],
   "source": [
    "pred_B_list = os.listdir(dirpath)\n",
    "pred_B_list = fnmatch.filter(pred_B_list, '*fake_B.png')\n",
    "print(len(pred_B_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hanover_000000_035606_leftImg8bit.png\n",
      "krefeld_000000_009574_leftImg8bit.png\n",
      "cologne_000033_000019_leftImg8bit.png\n",
      "monchengladbach_000000_012376_leftImg8bit.png\n",
      "strasbourg_000001_048121_leftImg8bit.png\n",
      "strasbourg_000001_057811_leftImg8bit.png\n",
      "weimar_000029_000019_leftImg8bit.png\n",
      "dusseldorf_000204_000019_leftImg8bit.png\n",
      "aachen_000139_000019_leftImg8bit.png\n",
      "bremen_000007_000019_leftImg8bit.png\n",
      "erfurt_000093_000019_leftImg8bit.png\n",
      "strasbourg_000000_004248_leftImg8bit.png\n",
      "tubingen_000037_000019_leftImg8bit.png\n",
      "stuttgart_000077_000019_leftImg8bit.png\n",
      "strasbourg_000001_010162_leftImg8bit.png\n",
      "krefeld_000000_019791_leftImg8bit.png\n",
      "hamburg_000000_020211_leftImg8bit.png\n",
      "hanover_000000_056800_leftImg8bit.png\n",
      "darmstadt_000079_000019_leftImg8bit.png\n",
      "aachen_000089_000019_leftImg8bit.png\n",
      "stuttgart_000104_000019_leftImg8bit.png\n",
      "cologne_000054_000019_leftImg8bit.png\n",
      "hamburg_000000_082187_leftImg8bit.png\n",
      "jena_000042_000019_leftImg8bit.png\n",
      "monchengladbach_000000_017950_leftImg8bit.png\n",
      "hanover_000000_030276_leftImg8bit.png\n",
      "hanover_000000_025335_leftImg8bit.png\n",
      "cologne_000068_000019_leftImg8bit.png\n",
      "cologne_000003_000019_leftImg8bit.png\n",
      "jena_000059_000019_leftImg8bit.png\n",
      "strasbourg_000000_017044_leftImg8bit.png\n",
      "weimar_000129_000019_leftImg8bit.png\n",
      "ulm_000078_000019_leftImg8bit.png\n",
      "weimar_000130_000019_leftImg8bit.png\n",
      "hanover_000000_036051_leftImg8bit.png\n",
      "zurich_000107_000019_leftImg8bit.png\n",
      "bochum_000000_003674_leftImg8bit.png\n",
      "krefeld_000000_035124_leftImg8bit.png\n",
      "stuttgart_000060_000019_leftImg8bit.png\n",
      "hamburg_000000_011641_leftImg8bit.png\n",
      "hanover_000000_046572_leftImg8bit.png\n",
      "stuttgart_000001_000019_leftImg8bit.png\n",
      "dusseldorf_000107_000019_leftImg8bit.png\n",
      "aachen_000042_000019_leftImg8bit.png\n",
      "hamburg_000000_019760_leftImg8bit.png\n",
      "hamburg_000000_074545_leftImg8bit.png\n",
      "strasbourg_000000_023064_leftImg8bit.png\n",
      "bremen_000196_000019_leftImg8bit.png\n",
      "bremen_000238_000019_leftImg8bit.png\n",
      "bremen_000199_000019_leftImg8bit.png\n",
      "krefeld_000000_010160_leftImg8bit.png\n",
      "weimar_000053_000019_leftImg8bit.png\n",
      "bremen_000237_000019_leftImg8bit.png\n",
      "monchengladbach_000000_009191_leftImg8bit.png\n",
      "ulm_000069_000019_leftImg8bit.png\n",
      "darmstadt_000064_000019_leftImg8bit.png\n",
      "ulm_000007_000019_leftImg8bit.png\n",
      "strasbourg_000001_005876_leftImg8bit.png\n",
      "stuttgart_000009_000019_leftImg8bit.png\n",
      "hamburg_000000_071016_leftImg8bit.png\n",
      "aachen_000130_000019_leftImg8bit.png\n",
      "strasbourg_000001_016376_leftImg8bit.png\n",
      "bremen_000271_000019_leftImg8bit.png\n",
      "stuttgart_000067_000019_leftImg8bit.png\n",
      "cologne_000142_000019_leftImg8bit.png\n",
      "stuttgart_000015_000019_leftImg8bit.png\n",
      "cologne_000139_000019_leftImg8bit.png\n",
      "strasbourg_000000_027771_leftImg8bit.png\n",
      "hamburg_000000_104428_leftImg8bit.png\n",
      "dusseldorf_000086_000019_leftImg8bit.png\n",
      "erfurt_000051_000019_leftImg8bit.png\n",
      "strasbourg_000000_013944_leftImg8bit.png\n",
      "strasbourg_000001_031976_leftImg8bit.png\n",
      "hanover_000000_047629_leftImg8bit.png\n",
      "zurich_000118_000019_leftImg8bit.png\n",
      "ulm_000046_000019_leftImg8bit.png\n",
      "bremen_000076_000019_leftImg8bit.png\n",
      "jena_000086_000019_leftImg8bit.png\n",
      "monchengladbach_000000_007695_leftImg8bit.png\n",
      "hamburg_000000_078407_leftImg8bit.png\n",
      "aachen_000036_000019_leftImg8bit.png\n",
      "strasbourg_000001_010445_leftImg8bit.png\n",
      "dusseldorf_000039_000019_leftImg8bit.png\n",
      "bochum_000000_018195_leftImg8bit.png\n",
      "aachen_000125_000019_leftImg8bit.png\n",
      "bochum_000000_021070_leftImg8bit.png\n",
      "bremen_000228_000019_leftImg8bit.png\n",
      "strasbourg_000001_022151_leftImg8bit.png\n",
      "stuttgart_000090_000019_leftImg8bit.png\n",
      "strasbourg_000001_008771_leftImg8bit.png\n",
      "strasbourg_000001_061685_leftImg8bit.png\n",
      "jena_000083_000019_leftImg8bit.png\n",
      "jena_000072_000019_leftImg8bit.png\n",
      "dusseldorf_000080_000019_leftImg8bit.png\n",
      "dusseldorf_000076_000019_leftImg8bit.png\n",
      "cologne_000136_000019_leftImg8bit.png\n",
      "aachen_000135_000019_leftImg8bit.png\n",
      "hamburg_000000_094717_leftImg8bit.png\n",
      "weimar_000074_000019_leftImg8bit.png\n",
      "bremen_000219_000019_leftImg8bit.png\n",
      "aachen_000084_000019_leftImg8bit.png\n",
      "cologne_000127_000019_leftImg8bit.png\n",
      "ulm_000005_000019_leftImg8bit.png\n",
      "dusseldorf_000005_000019_leftImg8bit.png\n",
      "weimar_000041_000019_leftImg8bit.png\n",
      "hanover_000000_008017_leftImg8bit.png\n",
      "aachen_000059_000019_leftImg8bit.png\n",
      "hanover_000000_039470_leftImg8bit.png\n",
      "aachen_000000_000019_leftImg8bit.png\n",
      "tubingen_000119_000019_leftImg8bit.png\n",
      "bremen_000223_000019_leftImg8bit.png\n",
      "cologne_000061_000019_leftImg8bit.png\n",
      "bremen_000261_000019_leftImg8bit.png\n",
      "krefeld_000000_016342_leftImg8bit.png\n",
      "monchengladbach_000001_000876_leftImg8bit.png\n",
      "ulm_000051_000019_leftImg8bit.png\n",
      "strasbourg_000000_030941_leftImg8bit.png\n",
      "bremen_000301_000019_leftImg8bit.png\n",
      "aachen_000034_000019_leftImg8bit.png\n",
      "aachen_000116_000019_leftImg8bit.png\n",
      "aachen_000134_000019_leftImg8bit.png\n",
      "jena_000094_000019_leftImg8bit.png\n",
      "hanover_000000_023881_leftImg8bit.png\n",
      "dusseldorf_000193_000019_leftImg8bit.png\n",
      "cologne_000075_000019_leftImg8bit.png\n",
      "hanover_000000_002357_leftImg8bit.png\n",
      "monchengladbach_000000_021104_leftImg8bit.png\n",
      "strasbourg_000001_032315_leftImg8bit.png\n",
      "strasbourg_000001_064224_leftImg8bit.png\n",
      "aachen_000120_000019_leftImg8bit.png\n",
      "bremen_000090_000019_leftImg8bit.png\n",
      "dusseldorf_000147_000019_leftImg8bit.png\n",
      "bochum_000000_038022_leftImg8bit.png\n",
      "hanover_000000_014537_leftImg8bit.png\n",
      "darmstadt_000034_000019_leftImg8bit.png\n",
      "ulm_000020_000019_leftImg8bit.png\n",
      "dusseldorf_000135_000019_leftImg8bit.png\n",
      "strasbourg_000000_027233_leftImg8bit.png\n",
      "stuttgart_000095_000019_leftImg8bit.png\n",
      "bremen_000174_000019_leftImg8bit.png\n",
      "bremen_000308_000019_leftImg8bit.png\n",
      "strasbourg_000000_028240_leftImg8bit.png\n",
      "bremen_000023_000019_leftImg8bit.png\n",
      "bremen_000064_000019_leftImg8bit.png\n",
      "aachen_000056_000019_leftImg8bit.png\n",
      "tubingen_000060_000019_leftImg8bit.png\n",
      "weimar_000104_000019_leftImg8bit.png\n",
      "aachen_000067_000019_leftImg8bit.png\n",
      "dusseldorf_000098_000019_leftImg8bit.png\n",
      "bremen_000135_000019_leftImg8bit.png\n",
      "bochum_000000_028297_leftImg8bit.png\n",
      "zurich_000090_000019_leftImg8bit.png\n",
      "cologne_000153_000019_leftImg8bit.png\n",
      "bremen_000117_000019_leftImg8bit.png\n",
      "tubingen_000121_000019_leftImg8bit.png\n",
      "dusseldorf_000209_000019_leftImg8bit.png\n",
      "dusseldorf_000064_000019_leftImg8bit.png\n",
      "aachen_000147_000019_leftImg8bit.png\n",
      "monchengladbach_000000_026602_leftImg8bit.png\n",
      "strasbourg_000001_051934_leftImg8bit.png\n",
      "aachen_000004_000019_leftImg8bit.png\n",
      "zurich_000009_000019_leftImg8bit.png\n",
      "monchengladbach_000000_022361_leftImg8bit.png\n",
      "stuttgart_000098_000019_leftImg8bit.png\n",
      "aachen_000083_000019_leftImg8bit.png\n",
      "strasbourg_000001_036480_leftImg8bit.png\n",
      "strasbourg_000001_013767_leftImg8bit.png\n",
      "bochum_000000_027951_leftImg8bit.png\n",
      "tubingen_000015_000019_leftImg8bit.png\n",
      "cologne_000040_000019_leftImg8bit.png\n",
      "stuttgart_000123_000019_leftImg8bit.png\n",
      "strasbourg_000001_034375_leftImg8bit.png\n",
      "weimar_000072_000019_leftImg8bit.png\n",
      "hamburg_000000_085073_leftImg8bit.png\n",
      "strasbourg_000000_013574_leftImg8bit.png\n"
     ]
    }
   ],
   "source": [
    "target_dir = '../results/Cycle_MidCity_RH/test_latest/images/pred_B'\n",
    "os.makedirs(target_dir, exist_ok=True)\n",
    "for name in pred_B_list:\n",
    "    img_num = cv2.imread(os.path.join(dirpath,name))\n",
    "    new_name = name[:-11]+'.png'\n",
    "#     print(new_name)\n",
    "    cv2.imwrite(os.path.join(target_dir, new_name), img_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hanover_000000_035606_leftImg8bit_fake_B.png'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hanover_000000_035606_leftImg8bit'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[:-11]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
